package com.cy;

import com.cy.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * @Author 张鹏宇
 * @Date 2021/3/28 16:33
 */
public class Test01 {
    public static void main(String[] args) throws Exception {
//        1.定义mybatis的主配置文件
        String config = "mybatis.xml";

//        2.读取这个文件config
        InputStream in = Resources.getResourceAsStream(config);

//        3.创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

//        4.创建SqlSessionFactory对象
        SqlSessionFactory factory = builder.build(in);

//        5.获取SqlSession对象，从SqlSessionFactory中获取Session对象
        SqlSession sqlSession = factory.openSession();

//        6.指定执行的SQL 语句
        String sqlid = "com.cy.dao.StudentDao" + "." + "selectFindAll";

//        7.执行SQL语句
        List<Student> selectList = sqlSession.selectList(sqlid);

//        8.遍历结果集(lambda表达式foreach)

        selectList.forEach(System.out :: println);

//        9.关闭资源
        sqlSession.close();

    }
}
